package cn.edu.zust.lxy.wangke_android;

import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import cn.edu.zust.lxy.wangke_android.util.HttpUtil;
import okhttp3.*;
import java.io.IOException;

public class DetailActivity extends AppCompatActivity {

    private Button btn_appoint;
    private Button btn_borrow;
    private String username;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);

        SharedPreferences sp = getSharedPreferences("user", MODE_PRIVATE);
        username = sp.getString("username","");
    }

    private void borrow(String username, String book_title) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    RequestBody requestbody = new FormBody.Builder()
                            .add("user_name", username)
                            .add("book_title", book_title)
                            .build();
                    Request request = new Request.Builder()
                            .url(HttpUtil.baseurl + "/book/borrow")
                            .post(requestbody)
                            .build();
                    Response response = client.newCall(request).execute();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(DetailActivity.this,"借阅成功！",Toast.LENGTH_SHORT).show();
                            btn_borrow.setEnabled(false);
                            btn_appoint.setEnabled(false);
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }).start();
    }

    private void appointment(String username, String book_title) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    RequestBody requestbody = new FormBody.Builder()
                            .add("user_name", username)
                            .add("book_title", book_title)
                            .build();
                    Request request = new Request.Builder()
                            .url(HttpUtil.baseurl + "/book/appointment")
                            .post(requestbody)
                            .build();
                    Response response = client.newCall(request).execute();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(DetailActivity.this,"预约成功！",Toast.LENGTH_SHORT).show();
                            btn_appoint.setEnabled(false);
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }).start();
    }

    @Override
    protected void onStart() {
        super.onStart();
        Intent intent = getIntent();
        String book_title = intent.getStringExtra("book_title");
        String book_author = intent.getStringExtra("book_author");
        String book_publish = intent.getStringExtra("book_publish");
        String book_profile = intent.getStringExtra("book_profile");
        String book_appointment = intent.getStringExtra("book_appointment");
        String book_borrow = intent.getStringExtra("book_borrow");

        TextView detail_title = findViewById(R.id.detail_title);
        TextView detail_author = findViewById(R.id.detail_author);
        TextView detail_publish = findViewById(R.id.detail_publish);
        TextView detail_profile = findViewById(R.id.detail_profile);
        btn_appoint = findViewById(R.id.appointment);
        btn_borrow = findViewById(R.id.borrow);

        detail_title.setText(book_title);
        detail_author.setText(book_author);
        detail_publish.setText(book_publish);
        detail_profile.setText(book_profile);
        if (book_appointment.equals("1") || book_borrow.equals("0")) {
            btn_appoint.setEnabled(false);
        }
        if (book_borrow.equals("1")) {
            btn_borrow.setEnabled(false);

        }
        btn_appoint.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                appointment(username, book_title);
            }
        });

        btn_borrow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                borrow(username, book_title);
            }
        });
    }
}